package com.example.demo.controller;

import com.example.demo.jejo.User;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

@RestController
public class RequestController {

    // 传统的JavaWeb的方法
    /*  @RequestMapping("/simpleParam")

    public String SimpleParam(HttpServletRequest request) {
        String name = request.getParameter("name");
        int age = Integer.parseInt(request.getParameter("age"));
        System.out.println(name + "的年龄是" + age);
        return "传输完成";
    }

     */
    //基于SpringBoot方式
    @RequestMapping("/simpleParam")
    public String SimpleParam(User user) {

        System.out.println(user.getName()
                + user.getAge()
                + user.address.getCounty()
                + user.address.getProvince()
                + user.address.getCity()
                + user.address.getDistrict());
        return "成功";
    }
    // 2.数组参数
    @RequestMapping("/arrayParam")
    public String ArrayParam(String[] hobby){
        System.out.println(Arrays.toString(hobby));
        return "ok";
    }
    //3.集合参数
    @RequestMapping("/listParam")
    public  String ListParam(@RequestParam List<String> hobby){
        System.out.println(hobby);
        return "ok";
    }

    //4.时间参数
    @RequestMapping("/dateTimeParam")
    public String DataTime(@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")LocalDateTime dateTime){
        System.out.println(dateTime);
        return "OK";
    }

    //5.json参数
    @RequestMapping("/jsonParam")
    public String jsonParam(@RequestBody User user){
        System.out.println(user);

        return "ok";

    }
    //6.路径参数
    @RequestMapping("/path/page={page}/size={size}")
    public String pathParam(@PathVariable Integer page,@PathVariable Integer size){
        System.out.println(page);
        System.out.println(size);
        return "ok";

    }


}
